import { PanelContentProvider, RegisterPanelDataCallback } from './interface';

/**
 * 面板全局
 *
 * @author chitanda
 * @date 2022-02-17 12:02:22
 * @export
 * @class PanelGlobal
 */
export class PanelGlobal {
  /**
   * 面板项适配器
   *
   * @author chitanda
   * @date 2022-02-16 20:02:53
   * @protected
   * @type {Map<string, PanelContentProvider>}
   */
  protected itemProviders: Map<string, PanelContentProvider> = new Map();

  /**
   * 面板数据项适配器
   *
   * @author chitanda
   * @date 2022-02-17 15:02:54
   * @protected
   * @type {Map<string, RegisterPanelDataCallback>}
   */
  protected dataProviders: Map<string, RegisterPanelDataCallback> = new Map();

  /**
   * 注册面板项适配器
   *
   * @author chitanda
   * @date 2022-02-16 20:02:40
   * @param {string} tag
   * @param {PanelContentProvider} provider
   */
  registerProvider(tag: string, provider: PanelContentProvider): void {
    this.itemProviders.set(tag, provider);
  }

  /**
   * 注销面板项适配器
   *
   * @author chitanda
   * @date 2022-02-16 20:02:48
   * @param {string} tag
   */
  unregisterProvider(tag: string): void {
    this.itemProviders.delete(tag);
  }

  /**
   * 注册数据适配器
   *
   * @author chitanda
   * @date 2022-02-17 15:02:11
   * @param {string} tag
   * @param {RegisterPanelDataCallback} provider
   */
  registerDataProvider(tag: string, provider: RegisterPanelDataCallback): void {
    this.dataProviders.set(tag, provider);
  }

  /**
   * 注销数据适配器
   *
   * @author chitanda
   * @date 2022-02-17 15:02:21
   * @param {string} tag
   */
  unregisterDataProvider(tag: string): void {
    this.dataProviders.delete(tag);
  }

  /**
   * 获取项适配器
   *
   * @author chitanda
   * @date 2022-02-17 14:02:42
   * @param {string} key
   * @return {*}  {(PanelContentProvider | undefined)}
   */
  getProvider(key: string): PanelContentProvider | undefined {
    return this.itemProviders.get(key);
  }

  /**
   * 获取所有适配器
   *
   * @author chitanda
   * @date 2022-02-17 15:02:10
   * @return {*}  {PanelContentProvider[]}
   */
  getAllProvider(): PanelContentProvider[] {
    return Array.from(this.itemProviders.values());
  }

  /**
   * 获取项适配器
   *
   * @author chitanda
   * @date 2022-02-17 15:02:24
   * @param {string} key
   * @return {*}  {(RegisterPanelDataCallback | undefined)}
   */
  getDataProvider(key: string): RegisterPanelDataCallback | undefined {
    return this.dataProviders.get(key);
  }

  /**
   * 获取所有数据适配器
   *
   * @author chitanda
   * @date 2022-02-17 15:02:37
   * @return {*}  {RegisterPanelDataCallback[]}
   */
  getAllDataProvider(): RegisterPanelDataCallback[] {
    return Array.from(this.dataProviders.values());
  }
}
// 面板全局注册中心
export const panel = new PanelGlobal();
